package com.sxt.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sxt.sys.SYSConstast.SYSConstast;
import com.sxt.sys.domain.Permission;
import com.sxt.sys.domain.Role;
import com.sxt.sys.service.PermissionService;
import com.sxt.sys.service.RoleService;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.utils.ResultObj;
import com.sxt.sys.utils.TreeNode;
import com.sxt.sys.vo.PermissionVo;
import com.sxt.sys.vo.RoleVo;

@RestController
@RequestMapping("role")
public class RoleController {
	
	
	@Autowired
	private RoleService roleService;
	
	
	@Autowired
	private PermissionService permissionService;
	//查询
	@RequestMapping("loadAllRole")
	public DataGridView loadAllRole(RoleVo roleVo) {
		return roleService.queryAllRole(roleVo);
	}
	
	@RequestMapping("deleteRole")
	public ResultObj deleteRole(RoleVo roleVo) {
		Integer code=1;
		String msg="";
		try {
			Integer info = roleService.deleteRole(roleVo.getId());
			code = info;
			msg = info > 0 ?SYSConstast.DELETE_LOGININFOR_TRUE:SYSConstast.DELETE_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.DELETE_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	@RequestMapping("addRole")
	public ResultObj addNotice(RoleVo RoleVo) {
		Integer code=1;
		String msg="";
		try {
			Integer info = roleService.addRole(RoleVo);
			code = info;
			msg = info > 0 ?SYSConstast.ADD_LOGININFOR_TRUE:SYSConstast.ADD_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.ADD_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	
	@RequestMapping("updateRole")
	public ResultObj updateNotice(RoleVo roleVo) {
		Integer code=1;
		String msg="";
		try {
			Integer info = roleService.updateRole(roleVo);
			code = info;
			msg = info > 0 ?SYSConstast.UPDATE_LOGININFOR_TRUE:SYSConstast.UPDATE_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.UPDATE_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	@RequestMapping("loadRolePermisisonTreeJosn")
	public List<TreeNode> loadRolePermisisonTreeJosn(RoleVo roleVo){
		List<TreeNode> node = new ArrayList<>();
		PermissionVo permissionVo=new PermissionVo();
		permissionVo.setAvailable(SYSConstast.MENU_AVAILABLE_TRUE);
		
		List<Permission> permissions = permissionService.queryAllPermissionForlist(permissionVo);
		
		List<Permission> permissions2 = permissionService.queryPermissionByRoleId(roleVo.getId());
		//建立树
		for (Permission p1 : permissions) {
			Boolean checked = false;
			for (Permission p2 : permissions2) {
				
				if(p1.getId()==p2.getId()) {
					checked = true;
					break;
				}
				
			}
			Boolean isParent = p1.getType().equals(SYSConstast.MENU_TYPE_MENU)?true:false;
			Boolean open = p1.getOpen()==SYSConstast.PERMISSION_OPEN_TRUE?true:false;
			node.add(new TreeNode(p1.getId(), p1.getPid(), p1.getName(), isParent, open, checked));
			
		}
		return node;
	}
	
	
	@RequestMapping("saveRolePermisison")
	public ResultObj saveRolePermisison(RoleVo roleVo) {
		Integer code=1;
		String msg="";
		try {
			Integer info = roleService.saveRolePermisison(roleVo);
			code = info;
			msg = info > 0 ?SYSConstast.OPERATE_DISPATCHE_SUCCESS:SYSConstast.OPERATE_DISPATCHE_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.OPERATE_DISPATCHE_FALSE;
		}
		return new ResultObj(code , msg);
	} 
}
